//
//  LELoginController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/9.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LELoginController.h"
#import "NSString+MD5.h"
#import "LEStoreModel.h"
#import <MJExtension/MJExtension.h>
#import "API.h"
#import "MakerTools.h"

#import "LETabBarViewController.h"
#import "LEForgetController.h"
#import "SelectLocaitonController.h"
#import "LENavigationController.h"

@interface LELoginController ()

@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *rememberButton;
@property (weak, nonatomic) IBOutlet UIButton *selectLocationButton;
/**
 记住密码
 */
@property (nonatomic, assign)BOOL isRemember;

@end

@implementation LELoginController

- (void)setupUI
{
    [super setupUI];
    self.title = @"登录";
    _isRemember = [[NSUserDefaults standardUserDefaults]boolForKey:IS_REMEMBER];
    if (!_isRemember) {
        self.userNameField.text = @"";
        self.passwordField.text = @"";
        [self.rememberButton setImage:[UIImage imageNamed:@"check_box"] forState:0];
    }else{
        NSString *useName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
        self.userNameField.text = useName;
        self.passwordField.text = password;
        [self.rememberButton setImage:[UIImage imageNamed:@"check_box_select"] forState:0];
    }
    
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLocation:) name:SELECT_LOCATION_NOTIFICATION object:nil];
    
    //设置地区
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:SAVE_LOCATION];
    if (dict)
    {
        [_selectLocationButton setTitle:dict[@"name"] forState:0];
        
    }else{
        [_selectLocationButton setTitle:@"请选择地区" forState:0];
    }

}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark- 网路请求
- (void)requestLogin
{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.userNameField.text forKey:@"seller_name"];
    [dict setObject:[self.passwordField.text stringToMD5] forKey:@"password"];
    [dict setObject:@"ios" forKey:@"client"];
    [SVProgressHUD showWithStatus:@"正在登录"];
    [API netAccess:LoginUrl andMethor:@"POST" andDict:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        NSString *code = successCode[@"code"];
        if ([code intValue] == 200) {
            NSDictionary *datas = successCode[@"datas"];
            LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
            NSString *key = datas[@"key"];
            NSString *error = datas[@"error"];
            if (error) {
                [SVProgressHUD showErrorWithStatus:error];
            }else
            {
                LEStoreModel *storeModel = [LEStoreModel mj_objectWithKeyValues:datas[@"store_info"]];
                //写入文件,保存用户信息
                if ([MakerTools saveToSandBox:Store_Info withSavedObject:storeModel])
                {
                    LELog(@"%s---%d---成功写入文件,保存用户信息",__func__,__LINE__);
                }
                //保存 key
                [[NSUserDefaults standardUserDefaults] setObject:key forKey:@"key"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                //注册极光信息
                [self requestJPushId];
                //设置根控制器
                LETabBarViewController *tabBarController = [[LETabBarViewController alloc] init];
                [UIApplication sharedApplication].delegate.window.rootViewController = tabBarController;
            }
        }
    } falure:^(NSError *er) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
    }];
}
- (void)requestJPushId
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *jpushId = [[NSUserDefaults standardUserDefaults]objectForKey:RegisterJPushId];
    NSString *jpushIdString = [NSString stringWithFormat:@"client_id|%@",jpushId];
    [params setObject:jpushIdString forKey:@"param"];
    
    [API requestPostAppendKey:RegisterJPushUrl params:params success:^(NSDictionary *successCode) {
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
        }
    } falure:^(NSError *er) {
        
    }];
}
#pragma mark- 点击事件
- (IBAction)rememberClick:(UIButton *)sender
{
    _isRemember = [[NSUserDefaults standardUserDefaults] boolForKey:IS_REMEMBER];
    
    UIImage *image = _isRemember ?[UIImage imageNamed:@"check_box"]:[UIImage imageNamed:@"check_box_select"];
    [sender setImage:image forState:0];
    _isRemember = !_isRemember;
    [[NSUserDefaults standardUserDefaults] setBool:_isRemember forKey:@"isRemember"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (IBAction)selectLocation:(UIButton *)sender {
    SelectLocaitonController *select = [[SelectLocaitonController alloc] init];
    LENavigationController *nav = [[LENavigationController alloc] initWithRootViewController:select];
    select.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nav animated:YES completion:nil];
}

- (IBAction)forgetClick:(UIButton *)sender
{
    LELog(@"%s---%d---忘记密码",__func__,__LINE__);
    LEForgetController *forget = [[LEForgetController alloc] init];
    [self.navigationController pushViewController:forget animated:YES];
}
- (IBAction)loginClick:(UIButton *)sender
{
    if (self.userNameField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"用户名不能为空"];
        
        return;
    }
    if (self.passwordField.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码的长度不能小于6位"];
        
        return;
    }

    //发送登录请求
    [[NSUserDefaults standardUserDefaults] setObject:self.userNameField.text forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults] setObject:self.passwordField.text  forKey:@"password"];
    [self requestLogin];
}
- (void)changeLocation:(NSNotification *)notification
{
    NSLog(@"%s---%d---%@",__func__,__LINE__,notification.object);
    NSDictionary *dict = notification.object;
    [self.selectLocationButton setTitle:dict[@"name"] forState:0];
    //存储地区
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:SAVE_LOCATION];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end










